package com.doubi.controller.api;

import com.doubi.common.aspectj.annotation.Delay;
import com.doubi.common.aspectj.annotation.NeedLogin;
import com.doubi.common.config.shiro.ShiroUtil;
import com.doubi.controller.common.BaseController;
import com.doubi.modelvo.sys.TSysUserVo;
import com.doubi.service.ApiUserService;
import com.doubi.vo.AppUiModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@CrossOrigin
@RequestMapping("Api/User")
@Api(value = "用户操作模块", tags = "用户操作模块")
public class ApiUserController extends BaseController {
    @Autowired
    private ApiUserService apiUserService;


    @ApiOperation(value = "通过账户密码的登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", required = true, dataType = "string"),
            @ApiImplicitParam(name = "password", value = "密码（sha1加密）", required = true, dataType = "string")
    })
    @PostMapping("Login")
    @ResponseBody
    public AppUiModel<TSysUserVo> login(String account, String password) {
        return apiUserService.login(account, password);
    }

    @ApiOperation(value = "通过账户验证码的登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", required = true, dataType = "string"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "string"),
            @ApiImplicitParam(name = "weixinid", value = "微信唯一标识", dataType = "string"),
            @ApiImplicitParam(name = "qqid", value = "QQ唯一标识", dataType = "string"),
            @ApiImplicitParam(name = "weiboid", value = "微博唯一标识", dataType = "string"),
            @ApiImplicitParam(name = "name", value = "昵称", dataType = "string"),
            @ApiImplicitParam(name = "head", value = "头像链接", dataType = "string")
    })
    @PostMapping("CodeLogin")
    @ResponseBody
    public AppUiModel<TSysUserVo> codelogin(String account, String code, String weixinid, String qqid, String weiboid,String name,String head) {
        return apiUserService.saveCodelogin(account, code, weixinid, qqid, weiboid,name,head);
    }

    @ApiOperation(value = "通过账户获取验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", required = true, dataType = "string"),
    })
    @PostMapping("VerificationCode")
    @ResponseBody
    public AppUiModel<String> verificationCode(String account) {
        return apiUserService.addVerificationCode(account, ShiroUtil.getIp());
    }

    @ApiOperation(value = "通过账户验证码修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", required = true, dataType = "string"),
            @ApiImplicitParam(name = "password", value = "密码（sha1加密）", required = true, dataType = "string"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "string"),
    })
    @PostMapping("ForgetPassword")
    @ResponseBody
    public AppUiModel<String> forgetPassword(String account, String password, String code) {
        return apiUserService.updateForgetPassword(account, password, code);
    }

    @ApiOperation(value = "通过微信账户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "weixinid", value = "微信账户", required = true, dataType = "string"),
    })
    @PostMapping("WeixinLogin")
    @ResponseBody
    public AppUiModel<TSysUserVo> weixinLogin(String weixinid) {
        return apiUserService.weixinLogin(weixinid);
    }

    @ApiOperation(value = "通过QQ账户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qqid", value = "qq账户", required = true, dataType = "string"),
    })
    @PostMapping("QqLogin")
    @ResponseBody
    public AppUiModel<TSysUserVo> qqLogin(String qqid) {
        return apiUserService.qqLogin(qqid);
    }

    @ApiOperation(value = "通过微博账户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "weiboid", value = "微博账户", required = true, dataType = "string"),
    })
    @PostMapping("WeiboLogin")
    @ResponseBody
    public AppUiModel<TSysUserVo> weiboLogin(String weiboid) {
        return apiUserService.weiboLogin(weiboid);
    }

    @ApiOperation(value = "绑定用户位置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "province", value = "省份", required = true, dataType = "string"),
            @ApiImplicitParam(name = "city", value = "市", required = true, dataType = "string"),
            @ApiImplicitParam(name = "area", value = "区", required = true, dataType = "string"),
    })
    @PostMapping("BindUserLocation")
    @ResponseBody
    @NeedLogin
    @Delay
    public AppUiModel<String> bindUserLocation(String province, String city, String area) {
        return apiUserService.updateBindUserLocation(super.getUserOid(), province, city, area);
    }


    @ApiOperation(value = "通过唯一标识获取个人信息")
    @GetMapping("UserBean")
    @ResponseBody
    @NeedLogin
    public AppUiModel<TSysUserVo> UserBean() {
        return apiUserService.updateGetUserBean(super.getUserOid());
    }

    @ApiOperation(value = "修改用户头像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "avatar", value = "头像", required = true, dataType = "string"),
    })
    @PostMapping("UpdateUserAvatar")
    @ResponseBody
    @NeedLogin
    @Delay
    public AppUiModel<String> updateUserAvatar(String avatar) {
        return apiUserService.updateUserAvatar(super.getUserOid(), avatar);
    }

    @ApiOperation(value = "修改用户昵称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "昵称", required = true, dataType = "string"),
    })
    @PostMapping("UpdateUserName")
    @ResponseBody
    @NeedLogin
    @Delay
    public AppUiModel<String> updateUserName(String name) {
        return apiUserService.updateUserName(super.getUserOid(), name);
    }

    @ApiOperation(value = "修改用户性别")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sex", value = "性别 1男 2女", required = true, dataType = "int"),
    })
    @PostMapping("UpdateUserSex")
    @ResponseBody
    @NeedLogin
    @Delay
    public AppUiModel<String> updateUserSex(Integer sex) {
        return apiUserService.updateUserSex(super.getUserOid(), sex);
    }

    @ApiOperation(value = "修改用户生日")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "birthday", value = "日期格式 yyyy-MM-dd", required = true, dataType = "string"),
    })
    @PostMapping("UpdateUserDate")
    @ResponseBody
    @NeedLogin
    @Delay
    public AppUiModel<String> updateUserDate(String birthday) {
        return apiUserService.updateUserDate(super.getUserOid(), birthday);
    }

    @ApiOperation(value = "修改用户密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "string"),
    })
    @PostMapping("UpdateUserPassword")
    @ResponseBody
    @NeedLogin
    @Delay
    public AppUiModel<String> updateUserPassword(String password) {
        return apiUserService.updateUserPassword(super.getUserOid(), password);
    }

    @ApiOperation(value = "修改用户手机号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "手机号", required = true, dataType = "string"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "string"),
    })
    @PostMapping("UpdateUserAccount")
    @ResponseBody
    @NeedLogin
    @Delay
    public AppUiModel<String> updateUserAccount(String account,String code) {
        return apiUserService.updateUserAccount(super.getUserOid(), account,code);
    }


    @ApiOperation(value = "查询账户是否有密码")
    @GetMapping("IsHavePassword")
    @ResponseBody
    @NeedLogin
    public AppUiModel<Integer> isHavePassword() {
        return apiUserService.isHavePassword(super.getUserOid());
    }

    @ApiOperation(value = "我的一级页面")
    @GetMapping("MyPage")
    @ResponseBody
    @NeedLogin
    public AppUiModel<Map<String,Object>> myPage() {
        return apiUserService.getMyPage(super.getUserOid());
    }


    @ApiOperation(value = "首页")
    @GetMapping("Home")
    @ResponseBody
    public AppUiModel<Map<String,Object>> home() {
        return apiUserService.getHome();
    }

}
